package com.zmn.oms.business.impl.log;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.brs.common.enums.EventEnum;
import com.zmn.brs.common.enums.PlatEnum;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.manager.mq.interfaces.ZmnMQSender;
import com.zmn.oms.business.interfaces.log.OrderLogOperateRecordBService;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.dto.OmsBaseOperator;
import com.zmn.oms.constant.MessageQueueTopicConstant;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.entity.log.OrderLog;
import com.zmn.oms.model.entity.work.OrderWork;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 类描述： 操作记录系统监控
 *
 * @author yule
 * @date 2021/4/16 18:25
 */
@Service
@Slf4j
public class OrderLogOperateRecordBServiceImpl implements OrderLogOperateRecordBService {
    @Resource
    private ZmnMQSender zmnMQSender;

    /**
     * 通知节点
     * 取消
     */
    static List<Integer> noticeTypes = Lists.newArrayList(
            OrderLogConsts.ORDER_LOG_TYPE_CANCEL
    );

    /**
     * 操作监控记录
     * @param orderLog
     * @param dbOrderWork
     * @param omsBaseOperator
     * @param proceed
     */
    @Override
    public void noticeRecord(OrderLog orderLog, OrderWork dbOrderWork, OmsBaseOperator omsBaseOperator, Object proceed) {
        try {
            if (!noticeTypes.contains(orderLog.getType())) {
                return;
            }
            String eventCode = "";
            // 取消监控 -- 暂时只监控员工操作
            if (Objects.equals(orderLog.getType(), OrderLogConsts.ORDER_LOG_TYPE_CANCEL) && Objects.equals(GlobalConsts.OPERATE_USER_TYPE_STAFF, omsBaseOperator.getOperatorType())) {
                eventCode = EventEnum.WORK_ORDER_CANCEL.name();
            }

            if (StringUtils.isBlank(eventCode)) {
                return;
            }

            // 安全监控
            String key = String.format("%s-record-%s", omsBaseOperator.getOperatorId(), DateUtil.getNowTimestamp());
            Map<String, Object> map = new HashMap<>();
            map.put("plat", PlatEnum.getPlatEnum(dbOrderWork.getPlat()).name());
            map.put("channelId", dbOrderWork.getChannelId());
            map.put("event", eventCode);
            map.put("objectIdentity", omsBaseOperator.getOperatorType());
            map.put("objectId", omsBaseOperator.getOperatorId());
            map.put("objectMobile", omsBaseOperator.getOperatorMobile());
            map.put("objectIp", omsBaseOperator.getIp());
            map.put("createTime", DateUtil.getNow());
            log.info("#oms#安全监控消息TOPIC = [{}] 入参{}", MessageQueueTopicConstant.ZMN_TOPIC_BRS, JSON.toJSONString(map));
            zmnMQSender.send(MessageQueueTopicConstant.ZMN_TOPIC_BRS, OmsMqTagConsts.BRS_TAG_ORDER_OPERATE_RECORD, key, JSON.toJSONString(map));
        } catch (Exception ex) {

        }
    }
}
